package Client;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import Server.server;



public class Receiver extends Thread {

	Socket clientSocket = null;
	
	public Receiver(Socket client) 
	{
		clientSocket = client;
	}
	
	public static void main(String[] args) throws UnknownHostException, IOException {

		
		ServerSocket rec = new ServerSocket(15001);
		System.out.println("Client Receiving Service Initiated");
		System.out.println("Waiting...");
		
		while(true) 
		{	     
			System.out.println("1");
			Socket connected = rec.accept();
			System.out.println("2");
            (new Receiver(connected)).start();
            
		} 
		
		
//		write();
	}
	
	public void run() {
		
		int filesize=100000000;//8609792;
	    int bytesRead=0;
	    int current = 0;
	    // localhost for testing
	    
//		ServerSocket rec = new ServerSocket(15000);
//		System.out.println("Client Receiving Service Initiated");
//		System.out.println("Waiting...");

		while(true)
	    {
	        
	   // Socket sock -clientSocket.accept();   
		System.out.println("Receiving Started...");
		BufferedReader in=null;
		try {
			in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		} catch (IOException e9) {
			// TODO Auto-generated catch block
			e9.printStackTrace();
		}
		String file=null;
		try {
			file = in.readLine();
		} catch (IOException e8) {
			// TODO Auto-generated catch block
			e8.printStackTrace();
		}
		String grp=null;
		try {
			grp = in.readLine();
		} catch (IOException e7) {
			// TODO Auto-generated catch block
			e7.printStackTrace();
		}
	    // receive file
	    byte [] mybytearray  = new byte [filesize];
	    BufferedInputStream din=null;
		try {
			din = new BufferedInputStream(clientSocket.getInputStream());
		} catch (IOException e6) {
			// TODO Auto-generated catch block
			e6.printStackTrace();
		} //.getDataInputStream();   //.getInputStream();
	    FileOutputStream fos= null;
		try {
			fos = new FileOutputStream(".\\" + grp + "\\" + file);
		} catch (FileNotFoundException e5) {
			// TODO Auto-generated catch block
			e5.printStackTrace();
		}
	    BufferedOutputStream bos = new BufferedOutputStream(fos);
	    try {
			bytesRead = din.read(mybytearray,0,mybytearray.length);
		} catch (IOException e4) {
			// TODO Auto-generated catch block
			e4.printStackTrace();
		}
	    current = bytesRead;

	    do 
	    {
	       try {
			bytesRead = din.read(mybytearray, current, (mybytearray.length-current));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	       if(bytesRead >= 0) current += bytesRead;
	    } while(bytesRead > -1);

	    try {
			bos.write(mybytearray, 0 , current);
		} catch (IOException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
	    try {
			bos.flush();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
	    try {
			bos.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	    
	    //sock.close();
	    System.out.println("File Received");
		System.out.println("Waiting For Next File...");	
////		try {
////			clientSocket.close();
////		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	  }
	}
}
